layui.use(['form','layer'], function(){
    var form = layui.form,
        layer = layui.layer,
        $=layui.$;

    $.ajax({
        url: config.gatewayUri+"/"+config.goodsServiceId+"/api/goods-category/admin/getAllGoodsCategoryList",
        type: "GET",
        dataType: "json",
        success: function(data){
            $.each(data, function (index, item) {
                $(".goodscategoryid").append(new Option(item.goodscategoryname,item.goodscategoryid));
            });
            layui.form.render("select");
        }
    });
    form.on('select(goodscategoryid)', function(data){
        $('.goodsid').html("");
        if(data.value==""){
            $(".goodsid").append(new Option("请选择",""));
            layui.form.render("select");
            return;
        }
        $.ajax({
            url: config.gatewayUri+"/"+config.goodsServiceId+'/api/goods/admin/getGoodsList',
            data:"goodscategoryid="+data.value,
            dataType: 'json',
            type: 'get',
            success: function (data) {
                $(".goodsid").append(new Option("请选择",""));
                $.each(data, function (index, item) {
                    $('.goodsid').append(new Option(item.goodsname, item.goodsid));// 下拉菜单里添加元素
                });
                layui.form.render("select");
                //重新渲染 固定写法
            }
        });
    });

    form.on('select(goodsid)', function(data){
        $('.goodsaccpoints').html("");
        $.ajax({
            url: config.gatewayUri+"/"+config.goodsServiceId+'/api/goods/admin/getAccpointsImg',
            data:"goodsid="+data.value,
            dataType: 'json',
            type: 'get',
            success: function (data) {
                $(".goodsaccpoints").append(data.goodsaccpoints);
                //重新渲染 固定写法
            }
        });
    });
    //提交审核通过
    form.on("submit(formDemo)",function(data){
        //弹出loading
        var index =layer.msg('数据提交中，请稍候',{icon: 16,time:false,shade:0.8});
        // 实际使用时的提交信息
        $.post(config.gatewayUri+"/"+config.goodsServiceId+"/api/goods/admin/saveOrUpdateGoods",{
            goodsid : $("#goodsid").val(),
            cabinetid :$("#cabinetid").val(),
        },function(res){
            layer.alert(res.msg);
        })
        setTimeout(function(){
            layer.close(index);
            layer.msg("新增商品成功！");
            //刷新父页面
            parent.location.reload();
        },500);
        return false;
    });
});